<?php
namespace app\admin\model;
class OrderTaoBao extends \think\Model{

	//订单逻辑
	public function order($order_array){

		array_splice($order_array, 0, 1);

		//更新订单表
		$this->tb_order( $order_array );

		//更新绑定表
		$this->wx_search_order();

		//处理事件
		$this->user_event();

		return "";
		

	}


	//更新订单表
	private function tb_order( $order ){
		

		//订单号数组
		$order_no = array()
		foreach ($order as $k => $v) {
			$order_no[$k] = $v['24']; 
		}

		//存在的订单
		$sql = "SELECT id,o_order_no,o_order_status FROM tb_order where o_order_no in(?)";
		$tr_to =  Db::query( $sql, implode( "," , $order_no ) );


		$add_order 			= array();
		$add_k  			= 0;
		$up_order_s_no 		= array();
		$up_order_j_no 		= array();
		$up_s 				= false;
		$up_j 				= false;
		$add_o				= false;
		
		foreach ($order as $k => $v) {

			foreach ($tr_to as $ke => $va) {
				
				//更新的订单
				if( $v[24] == $va['o_order_no'] ){

					if($v[8] == "订单失效" && $va['o_order_status'] != "订单失效"){
						$up_order_s_no[] 		= $v[24];
						$up_s = true;
					}
					if($v[8] == "订单结算" && $va['o_order_status'] != "订单结算"){
						$up_order_j_no[] 		= $v[24];
						$up_j = true;
					}
					
				}

				//全新的订单
				if( $v[24] != $va['o_order_no'] ){
					$add_order[$add_k][] = $v;
					$add_k++;
					$add_o = true;
				}

			}
			
		}

		//更新订单状态
		if( $up_s ){
			$sql = "UPDATE tb_order SET o_order_status = ? ,  status = ? WHERE o_order_no in(?) ";
        	Db::query( $sql, array( "订单失效" , "0" , implode( "," , $up_order_s_no ) ) );
		}
		
		if( $up_j ){
			$sql = "UPDATE tb_order SET o_order_status = ? ,  status = ? WHERE o_order_no in(?) ";
        	Db::query( $sql, array( "订单结算" , "0" , implode( "," , $up_order_j_no ) ) );
		}
        


		//插入全新的订单
		if ( $add_o ){

			$i = 0;
			foreach ($add_order as $k => $v) {
				//插入订单
				$data = array();
				$data[$i]['t_creat_time']  		= $v[0];	//创建时间
				$data[$i]['t_click_time']  		= $v[1];	//点击时间
				$data[$i]['s_goods_name']  		= $v[2];	//商品信息
                $data[$i]['s_goods_id'] 		= $v[3];	//商品ID
                $data[$i]['s_ww_name']  		= $v[4];	//旺旺名称
                $data[$i]['s_shop_name']    	= $v[5];	//店铺名称
                $data[$i]['s_goods_num']  		= $v[6];	//商品数量
                $data[$i]['s_goods_price']  	= $v[7];	//商品单价
                $data[$i]['o_order_status']  	= $v[8];	//订单状态
                $data[$i]['o_order_type']  		= $v[9];	//订单类型
                $data[$i]['o_sr_percent']  		= $v[10];	//收入比列
                $data[$i]['o_fc_percent']  		= $v[11];	//分成比列
                $data[$i]['o_order_money']  	= $v[12];	//付款金额
                $data[$i]['o_guji_xiaoguo']  	= $v[13];	//预估效果
                $data[$i]['o_end_money']  		= $v[14];	//结算金额
                $data[$i]['o_guji_shouru']  	= $v[15];	//预估收入
                $data[$i]['o_end_time']  		= $v[16];	//结算时间
                $data[$i]['o_yj_percent']  		= $v[17];	//佣金比列
                $data[$i]['o_yj_money']  		= $v[18];	//佣金金额
                $data[$i]['o_butie_percent']	= $v[19];	//补贴比列
                $data[$i]['o_butie_money']  	= $v[20];	//补贴金额
                $data[$i]['o_butie_type']  		= $v[21];	//补贴类型
                $data[$i]['o_deal_type']  		= $v[22];	//成交平台
                $data[$i]['l_three_name']  		= $v[23];	//第三方服务来源
                $data[$i]['o_order_no']  		= $v[24];	//订单编号
                $data[$i]['s_cat_name']  		= $v[25];	//类目名称
                $data[$i]['l_media_id']  		= $v[26];	//来源媒体ID
                $data[$i]['l_media_name']  		= $v[27];	//来源媒体名称
                $data[$i]['l_ad_id']  			= $v[28];	//广告位ID
                $data[$i]['l_ad_name']  		= $v[29];	//广告位名称
                $data[$i]['t_cj_time']  		= time();	//采集时间
                $data[$i]['status']  			= 0;		//结算状态
                $i++;		
			}
            Db::name('tb_order')->insertAll($data); 

		}
	}



	//更新绑定表
	private function wx_search_order(){

		//获取今天的订单
		$sql = "SELECT id,o_order_no,o_order_status FROM tb_order where status = ?";
		$tb_order =  Db::query( $sql, array( 0 ) );

		$tb_order_no = array();
		$tb_status_f = array();
		$tb_status_s = array();
		$tb_status_j = array();
		$f_k 		 = 0;
		$s_k 		 = 0;
		$j_k 		 = 0; 
		foreach ($tb_order as $k => $v) {
			$tb_order_no[] 					= $v['o_order_no'];		//订单号

			//订单付款 
			if( $v['o_order_status'] == "订单付款" ){
				$tb_status_f[$f_k]['o_order_no']		=	$v['o_order_no'];
				$f_k++;
			} 

			//订单失效 
			if( $v['o_order_status'] == "订单失效" ){
				$tb_status_s[$s_k]['o_order_no']		=	$v['o_order_no'];
				$s_k++;
			} 

			//订单结算 
			if( $v['o_order_status'] == "订单结算" ){	
				$tb_status_j[$j_k]['o_order_no']		=	$v['o_order_no'];	
				$j_k++;
			} 
		}

		//获取要处理的绑定信息
		$sql = "SELECT user_id,uuid,tb_order_no,status FROM wx_search_order where tb_order_no in (?)";
		$wx_search_order =  Db::query( $sql, array( implode("," , $tb_order_no) ) );

		$wso_f = array();
		$wso_s = array();
		$wso_j = array();
		$f = false;
		$s = false;
		$j = false;
		foreach ($wx_search_order as $k => $v) {
			
			//订单付款
			foreach ($tb_status_f as $ke => $va) {
				
				if($v['status'] == 0 && $v['tb_order_no'] == $va['o_order_no'] ){	
					$wso_f[] = $v['tb_order_no'];	
					$f = true;
				}

			}

			//订单失效
			foreach ($tb_status_s as $ke => $va) {
			
				if( $v['tb_order_no'] == $va['o_order_no'] ){	
					$wso_s[] = $v['tb_order_no'];	
					$s = true;
				}

			}	

			//订单结算
			foreach ($tb_status_j as $ke => $va) {
				
				if( $v['tb_order_no'] == $va['o_order_no'] ){	
					$wso_j[] = $v['tb_order_no'];	
					$j = true;
				}

			}				
		}

		//更改状态
		if( $f ){
			$sql = "UPDATE wx_search_order SET status = ? ,  update_time = ? , type = ? WHERE tb_order_no in(?) ";
        	Db::query( $sql, array( 1 , time() , 0 ,  implode( "," , $wso_f ) ) );

        	$sql = "UPDATE tb_order SET status = ? WHERE o_order_no in(?) ";
        	Db::query( $sql, array( "1" , implode( "," , $wso_f ) ) );
		}

		if( $s ){
			$sql = "UPDATE wx_search_order SET status = ? ,  update_time = ? , type = ? WHERE tb_order_no in(?) ";
        	Db::query( $sql, array( 2 , time() , 0 ,  implode( "," , $wso_s ) ) );

        	$sql = "UPDATE tb_order SET status = ? WHERE o_order_no in(?) ";
        	Db::query( $sql, array( "1" , implode( "," , $wso_s ) ) );
		}

		if( $j ){
			$sql = "UPDATE wx_search_order SET status = ? ,  update_time = ? , type = ? WHERE tb_order_no in(?) ";
        	Db::query( $sql, array( 3 , time() , 0 ,  implode( "," , $wso_j ) ) );

        	$sql = "UPDATE tb_order SET status = ? WHERE o_order_no in(?) ";
        	Db::query( $sql, array( "1" , implode( "," , $wso_j ) ) );
		}

	}

	//处理事件
	private function user_event(){

		//获取要处理的绑定信息
		$sql = "SELECT a.status , a.uuid , a.tb_order_no , a.user_id , b.o_order_money , c.name FROM wx_search_order as a LEFT JOIN tb_order as b ON a.tb_order_no = b.o_order_no LEFT JOIN user as c ON a.user_id = c.id WHERE a.type = 0";
		$wx_search_order =  Db::query( $sql );

		

		//事件

		$k 				= 0;
		$task_msg 		= array();
		$order_no  		= array();
		foreach ($wx_search_order as $k => $v) {
				
			if(  $v['status'] == 1	){

				$str = 	"尊敬的【".$v['name']."】会员 \n";
                $str .= "你的订单【".$v['tb_order_no']."】 \n";
                $str .= "已付款";

		        $data = [
		            "touser" => $v['uuid'],
		            "msgtype"=> "text",
		            "content"=> $str,
		        ];

		        $task_msg[$k]['content']  = json_decode($data);
		        $task_msg[$k]['user_id']  = $v['user_id'];
		        $task_msg[$k]['order_no'] = $v['tb_order_no'];
		        $k++;
		        $order_no[] = $v['tb_order_no'];
			}
			
			if(  $v['status'] == 2	){

				$str = 	"尊敬的【".$v['name']."】会员 \n";
                $str .= "你的订单【".$v['tb_order_no']."】 \n";
                $str .= "已失效";

		        $data = [
		            "touser" => $v['uuid'],
		            "msgtype"=> "text",
		            "content"=> $str,
		        ];

		        $task_msg[$k]['content']  = json_decode($data);
		        $task_msg[$k]['user_id']  = $v['user_id'];
		        $task_msg[$k]['order_no'] = $v['tb_order_no'];
		        $k++;
		        $order_no[] = $v['tb_order_no'];
			}

			if(  $v['status'] == 3	){

				$str = 	"尊敬的【".$v['name']."】会员 \n";
                $str .= "你的订单【".$v['tb_order_no']."】 \n";
                $str .= "已结算,本次获得【".$v['o_order_money']."】积分";

		        $data = [
		            "touser" => $v['uuid'],
		            "msgtype"=> "text",
		            "content"=> $str,
		        ];

		        $task_msg[$k]['content']  = json_decode($data);
		        $task_msg[$k]['user_id']  = $v['user_id'];
		        $task_msg[$k]['order_no'] = $v['tb_order_no'];
		        $k++;
		        $order_no[] = $v['tb_order_no'];

		        //修改积分
		        $sql = "UPDATE user SET shopping_points = shopping_points + ? WHERE id = ? ";
        		Db::query( $sql, array( $v['o_order_money'] , $v['user_id'] ) );
			}
		}

		//修改绑定信息
		$sql = "UPDATE wx_search_order SET type = ? WHERE tb_order_no in (?)  ";
        Db::query( $sql, array( 1 , implode( "," , $order_no ) ) );

        //删除推送消息
        $sql = "DELETE FROM wx_task_msg WHERE order_no in (?)"
        Db::query( $sql, array( implode( "," , $order_no ) ) );

        //插入推送消息
        Db::name('wx_task_msg')->insertAll($task_msg);

	}
}